#!/usr/bin/env python

import sys
import os
import zipfile

def main(args=None):
    if args == None:
        args = sys.argv[1:]

    if len(args) == 0:
        print 'Usage:\r\n\t' + sys.argv[0], r'\\marlin\scm-system\scm-depots\component-depot\agfahc-pacs-services-setup', r'\\vam7011\pacs-assemblies'
        print '\t\tor'
        print '\t' + sys.argv[0], r'\\vam7011\pacs-assemblies'
        return 0
    
    if len(args) > 1:
        sourcePath = args[0]
        args = args[1:]
    else:
        sourcePath = r'\\marlin\scm-system\scm-depots\component-depot\agfahc-pacs-services-setup'
        
    if len(args) > 0:
        outputDir = args[0] # \\vam7011\pacs-assemblies
    else:
        outputDir = '.'

    def addFile(arg, dirname, fnames):
        for fname in fnames:
            fullPath = os.path.join(dirname, fname)
            if os.path.isfile(fullPath):
                alias = fullPath[len(arg[0])+1:]
                print 'adding', alias
                arg[1].write(fullPath, alias)
        
    matchString = 'agfahc-pacs-services-setup_main_build'
    rests = []
    for thing in os.listdir(sourcePath):
        if 0 == thing.find(matchString):
            #print thing[len(matchString):]
            rests.append(thing[len(matchString):])
            
    rests = map(int, rests)
    rests.sort()

    dirToCopy = os.path.join(sourcePath, matchString + str(rests[-1]))

    print 'copying', dirToCopy
    z = zipfile.ZipFile(os.path.join(outputDir, matchString + str(rests[-1]) + '.zip'), 'w', compression=zipfile.ZIP_DEFLATED)

    os.path.walk(os.path.join(dirToCopy, 'lib'), addFile, (dirToCopy, z))

    z.close()
    return 0


if __name__ == '__main__':
    sys.exit(main())

